/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/9/8
 * Time: 15:09
 */
$(function(){

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-role"><option value="">选择角色</option><option value="1">教师</option><option value="2">人事处</option><option value="3">计财处</option><option value="4">资产管理处</option><option value="5">超级管理员</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/role/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-role');

    $('#edit-role-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var number = button.data('number');
        var realname = button.data('realname');
        var role = button.data('role');
        $(this).find('.user-id').val(id);
        $(this).find('.user-number').text(number);
        $(this).find('.user-name').text(realname);
        $(this).find('.user-select-role').val(role);
        $(this).find('.user-select-role option').each(function(){
            if ($(this).val() == role) {
                $(this).attr('selected', 'selected');
            }
        });
    });
    $('#edit-role-dialog').on('hide.bs.modal', function(){
        $(this).find('.user-id').val('');
        $(this).find('.user-number').text('');
        $(this).find('.user-name').text('');
        $(this).find('.user-select-role option:selected').removeAttr('selected');
        $(this).find('.user-select-role option:first').attr('selected', 'selected');
    });

    $('#edit-role-dialog .btn-save').on('click', function(){
        var that = $('#edit-role-dialog');
        var data = {
            id: that.find('.user-id').val(),
            role: that.find('.user-select-role').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/change/role', 'post', {data:data}, function(json){
                that.modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});


function loadList(url, toolbar) {
    $("#role-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.role = $('.select-role').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'role',
            title: '角色',
            formatter: function (value) {
                return getRole(value);
            }
        }, {
            field: 'number',
            title: '工号'
        }, {
            field: 'realname',
            title: ' 姓名'
        }, {
            field: 'gender',
            title: '性别',
            formatter: function (value) {
                return value == 1 ? '男' : '女';
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                return '<a class="btn btn-xs btn-success edit-role" data-toggle="modal" data-target="#edit-role-dialog" data-id="'+value+'" data-number="'+row.number+'" data-realname="'+row.realname+'" data-role="'+row.role+'">编辑角色</a>';
            }
        }]
    });
}

function refreshList() {
    $('#role-table').bootstrapTable('refresh');
}

function getRole(role) {
    switch (role) {
        case 1 : return '<span class="text-default"><b>教师</b></span>';
        case 2 : return '<span class="text-success"><b>人事处</b></span>';
        case 3 : return '<span class="text-info"><b>计财处</b></span>';
        case 4 : return '<span class="text-warning"><b>资产管理处</b></span>';
        case 5 : return '<span class="text-danger"><b>超级管理员</b></span>';
        default : return '<span class="text-default"><b>教师</b></span>';
    }
}

function checkForm(data) {
    return true;
}